/**
 * @author gao
 * @date 2025/7/1 16:56
 */

public class Demo3 {
    static class Counter {
        private int count = 0;
        private Object lock = new Object();
        /*synchronized public void Add() {
            count++;
        }*/

        public void Add() {
            synchronized (this) {
                count++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50_000; i++) {
                counter.Add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50_000; i++) {
                counter.Add();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(counter.count);
    }
}